package com.lhm.service;

import com.lhm.entity.Cart;
import com.lhm.entity.CartBook;
import com.lhm.mapper.BookMapper;
import com.lhm.mapper.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    CartMapper cartMapper;

    @Autowired
    BookMapper bookMapper;
    @Override
    public Cart findOne(int id) {
        return cartMapper.findOne(id);
    }

    @Override
    public List<CartBook> findByUid(int uid) {
        return cartMapper.findByUid(uid);
    }

    @Override
    public void remove(int id) {
        cartMapper.remove(id);
    }

    @Override
    public void update(int id, int num) {
            cartMapper.update(id,num);
    }

    @Override
    @Transactional//开始事务
    public void add(Cart cart){
        bookMapper.addFavnum(cart.getBookid(), 1);
        Cart c = cartMapper.findByBookidUid(cart.getUid(), cart.getBookid());
        cart.setTcreate(new Date());
        if (c == null) {
            cartMapper.add(cart);
        }
    }
    @Override
    public Cart findByBookidUid(int uid, int bookid) {
        return cartMapper.findByBookidUid(uid,bookid);
    }
}
